package org.zzh.controller.system.role;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.zzh.controller.BaseController;
import org.zzh.dao.common.PageBean;
import org.zzh.domain.system.dept.PeDept;
import org.zzh.domain.system.module.SsModule;
import org.zzh.domain.system.role.PeRole;
import org.zzh.domain.system.user.PeUser;
import org.zzh.service.system.module.SsModuleService;
import org.zzh.service.system.role.PeRoleService;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/role")
public class PeRoleController extends BaseController {

    @Autowired
    private PeRoleService peRoleService;

    @Autowired
    private SsModuleService ssModuleService;

    @RequestMapping(value = "/list.do")
    public ModelAndView list(
            @RequestParam(required = false, value = "page", defaultValue = "1")
            Integer page,
            @RequestParam(required = false, value = "pageSize", defaultValue = "5")
            Integer pageSize,
            @RequestParam(required = false, value = "name", defaultValue = "")
            String name
    ) {
        ModelAndView modelAndView = new ModelAndView();
        PageBean<PeRole> list = peRoleService.list(page, pageSize, name);
        modelAndView.addObject("pageBean", list);
        modelAndView.addObject("name", name);
        modelAndView.setViewName("/WEB-INF/pages/system/role/role-list");
        return modelAndView;
    }

    @RequestMapping(value = "/toAdd.do")
    public ModelAndView toAdd() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/WEB-INF/pages/system/role/role-add");
        return modelAndView;
    }

    @RequestMapping(value = "toUpdate.do")
    public ModelAndView toUpdate(String id) {
        ModelAndView modelAndView = new ModelAndView();
        PeRole role = peRoleService.findById(id);
        modelAndView.addObject("role", role);
        modelAndView.setViewName("/WEB-INF/pages/system/role/role-update");
        return modelAndView;
    }

    @RequestMapping(value = "edit.do")
    public String toEdit(PeRole role) {
        if (role.getRoleId() != null && !role.getRoleId().isEmpty()) {
            peRoleService.update(role);
            return "redirect:/system/role/list.do";
        }
        PeDept dept = (PeDept) session.getAttribute("company");
        if (dept != null) {
            role.setCompanyId(dept.getCompanyId());
            role.setCompanyName(dept.getCompanyName());
        } else {
            role.setCompanyId(companyId);
            role.setCompanyName(companyName);
        }
        PeUser user = (PeUser) session.getAttribute("user");
        if (user != null) {
            role.setCreateBy(user.getUserId());
            role.setCreateDept(user.getDeptId());
        } else {
            role.setCreateBy(userId);
            role.setCreateDept(deptId);
        }
        role.setRoleId(UUID.randomUUID().toString());
        peRoleService.add(role);
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "delete.do")
    public String delete(String[] ids) {
        peRoleService.deleteList(ids);
        ssModuleService.deleteRoleModuleList(ids);
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "roleModule.do")
    public ModelAndView roleModule(String roleId) {
        ModelAndView modelAndView = new ModelAndView();
        List<SsModule> moduleList = ssModuleService.list();
        List<SsModule> roleModuleList = ssModuleService.listByRoleId(roleId);
        PeRole role = peRoleService.findById(roleId);
        modelAndView.addObject("role", role);
        modelAndView.addObject("moduleList", moduleList);
        modelAndView.addObject("roleModuleList", roleModuleList);
        modelAndView.setViewName("/WEB-INF/pages/system/role/role-module");
        return modelAndView;
    }

    @RequestMapping(value = "updateRoleModule.do")
    public String updateRoleModule(String roleId, String[] moduleIds) {
        ssModuleService.updateRoleModuleList(roleId, moduleIds);
        return "redirect:/system/role/list.do";
    }
}
